//
//  SSListConentViewController.m
//  starspaceVR
//
//  Created by 曹端芹 on 17/9/22.
//  Copyright © 2017年 starspace. All rights reserved.
//

#import "SSListConentViewController.h"
#import "SSApplicDetailViewController.h"
#import "SSHandPickViewCell.h"
#import "SSHandpickModel.h"

@interface SSListConentViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) NSMutableArray *dataSource;

@end

static NSString *const handpickViewCellID = @"handpickViewCellID";
@implementation SSListConentViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupCollectionView];
   

}

- (void) setupCollectionView {
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requestData];
    }];
    [self.collectionView.mj_header beginRefreshing];
    
}

- (void) requestData {
    
    [self showHUD];
    NSDictionary *param = @{@"categoryId":@(self.categoryId),@"platform":@(3),@"quantity":@(20)};
    
    [SSNetworkManager getWithURLString:kSSHandPickListAPI parameters:param success:^(NSDictionary *responseDict) {
        if ([self.collectionView.mj_header isRefreshing]) {
            [self.dataSource removeAllObjects];
        }
        for (NSDictionary *dict in responseDict[@"data"]) {
            SSHandpickModel *model = [SSHandpickModel mj_objectWithKeyValues:dict];
            [self.dataSource addObject:model];
        }
        [self.collectionView reloadData];
        [self.collectionView.mj_header endRefreshing];
        [self dismissHUD];
    } failure:^(NSError *error) {
         [self.collectionView.mj_header endRefreshing];
        [self dismissHUD];
    }];
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SSHandPickViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:handpickViewCellID forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SSApplicDetailViewController *detailViewContrller = [[SSApplicDetailViewController alloc] init];
    detailViewContrller.guid = [self.dataSource[indexPath.row] guid];
    [self.navigationController pushViewController:detailViewContrller animated:YES];
        
    
}


- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        flowLayout.minimumLineSpacing = 5;
        flowLayout.minimumInteritemSpacing = 5;
        flowLayout.itemSize = CGSizeMake( (kScreenWidth - 40 ) / 2, 120);
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [_collectionView registerClass:[SSHandPickViewCell class] forCellWithReuseIdentifier:handpickViewCellID];
    }
    return _collectionView;
}



- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
